package com.zwy.core.controller;


import cn.hutool.core.util.ObjectUtil;
import com.zwy.common.utils.ResponseResult;
import com.zwy.core.domain.dto.LoginDTO;
import com.zwy.core.domain.entity.TbUser;
import com.zwy.core.domain.vo.LoginVO;
import com.zwy.core.service.ITbUserService;
import com.zwy.core.utils.JwtUtils;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.stream.Collectors;

import static com.mysql.cj.conf.PropertyKey.logger;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周哥
 * @since 2025-06-08
 */
@RestController
@RequestMapping("/user/auth")
public class TbUserController {


//    private final AuthenticationManager authenticationManager;
//
//    private final JwtUtils jwtUtils;
//
//    private final ITbUserService userService;
//
//    public TbUserController(AuthenticationManager authenticationManager,
//                            JwtUtils jwtUtils,
//                            ITbUserService userService){
//        this.authenticationManager = authenticationManager;
//        this.jwtUtils = jwtUtils;
//        this.userService = userService;
//    }
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private ITbUserService userService;
    private static final Logger logger = LoggerFactory.getLogger(TbUserController.class);
    @PostMapping("/login")
    public ResponseResult login(@RequestBody @Valid LoginDTO request){

        logger.info("Received login request: {}", request);
        //1.验证用户名和密码
        Authentication authentication = authenticationManager.authenticate(
                new UsernamePasswordAuthenticationToken(request.getUsername(),
                        request.getPassword())
        );
        //2.生成jwt
        String token = jwtUtils.generateToken(authentication);
        //3.获取用户信息
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        TbUser user =  userService.findByUsername(userDetails.getUsername());
        //4.构建响应
        LoginVO response = new LoginVO();
        response.setToken(token);
        response.setExpiresIn(jwtUtils.getExpirationTime());
        LoginVO.UserInfo userInfo = new LoginVO.UserInfo();
        userInfo.setId(user.getId());
        userInfo.setUsername(user.getUsername());
        userInfo.setRoles(userDetails.getAuthorities().stream()
                .map(GrantedAuthority::getAuthority)
                .collect(Collectors.toList()));
        response.setUserInfo(userInfo);
        return ResponseResult.success(response);
    }

}
